﻿package views {
	import at.geoathome.ui.buttons.Button;
	import Box2D.Common.Math.b2Vec2;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import Box2D.Dynamics.b2Body;	

	/**
	* ...
	* @author Georg Kamptner
	*/
	

	
	public class InGameView extends Sprite
	{
		
		// -- variables & attributes ---------------------------------------------------------------------------
		private var _drawn:Boolean;
		private var _game:Game;
		// visual
		private var _player1_title:Sprite;
		private var _player2_title:Sprite;
		private var _player1_height:MovieClip;
		private var _player2_height:MovieClip;
		
		private var _player1_heightLine:MovieClip;
		private var _player2_heightLine:MovieClip;
		private var _swapPlayerMsg:MovieClip;
		private var _quit_btn:Button;
		private var _next_player_btn:Button;
		private var _winner_line:Sprite;
		private var _winner_screen:Sprite;
		private var _icons:Sprite;
		private var _iconsList:Array;
		private var _text_manager:TextManager=new TextManager();
		
		private static const TEXT_COLOR_RED:uint = 0xEE4441;
		private static const TEXT_COLOR_BLUE:uint = 0x385BB8;
		private static const TEXT_COLOR_LIGHT_RED:uint = 0xFFC4C4;
		private static const TEXT_COLOR_LIGHT_BLUE:uint = 0xB4B5FE;
		
		
		// -- constructor --------------------------------------------------------------------------------------
		public function InGameView(game:Game)
		{
			_game = game;
			_drawn = false;
			extractAssets();
			hide();
			_icons = new Sprite();
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		
		// -- methods ------------------------------------------------------------------------------------------
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			if (visible)
				show();
		}
		
		private function extractAssets():void
		{
			_player1_title		= new (_game.asset_loader.getDefinitionOf("PlayerName"))();
			_player2_title		= new (_game.asset_loader.getDefinitionOf("PlayerName"))();
			_player1_height     = new (_game.asset_loader.getDefinitionOf("Player1height"))();
			_player2_height     = new (_game.asset_loader.getDefinitionOf("Player2height"))();
			_player1_heightLine = new (_game.asset_loader.getDefinitionOf("Player1heightLine"))();
			_player2_heightLine = new (_game.asset_loader.getDefinitionOf("Player2heightLine"))();
			_quit_btn			= new Button("QuitBtnBackground");
			_next_player_btn	= new Button("NextPlayertBtnBackground");
			_winner_line		= new (_game.asset_loader.getDefinitionOf("WinnerLine"))();
			_winner_screen      = new (_game.asset_loader.getDefinitionOf("WinnerScreen"))();
			_swapPlayerMsg		= new (_game.asset_loader.getDefinitionOf("swapPlayerMsg"))();
		}
		
		
		private function draw():void 
		{
			// player I Name
			_player1_title.x = 5;
			_player1_title.y = 5;
			TextField(_player1_title.getChildByName("txt"))..textColor = TEXT_COLOR_RED;
			TextField(_player1_title.getChildByName("txt")).text =  _game.Player1Name;
			addChild(_player1_title);
			
			// player II Name
			  var f:TextFormat = new TextFormat();
			  f.align = TextFormatAlign.RIGHT;
			_player2_title.x = stage.stageWidth - _player2_title.width - 5
			_player2_title.y = 5;
			TextField(_player2_title.getChildByName("txt")).textColor = TEXT_COLOR_BLUE;
			TextField(_player2_title.getChildByName("txt")).defaultTextFormat = f;
			TextField(_player2_title.getChildByName("txt")).text = _game.Player2Name;
			addChild(_player2_title);
			
			// Tower Height Textfield (Player I)
			_player1_height.x = 0 ;
			_player1_height.y = 70;
			TextField(_player1_height.getChildByName("height1")).text = _game.Player1TopHeight.toString();
			addChild(_player1_height);
			_player1_height.visible=false;
			
			// Tower Height Textfield (Player II)
			_player2_height.x = stage.stageWidth;
			_player2_height.y = 70;
			TextField(_player2_height.getChildByName("height2")).text = _game.Player2TopHeight.toString();
			addChild(_player2_height);
			_player2_height.visible=false;
			
			
			_player1_heightLine.x = 0 ;
			_player1_heightLine.y = 70;
			addChild(_player1_heightLine);
			_player1_heightLine.visible=false;
			
			_player2_heightLine.x = 0;
			_player2_heightLine.y = 70;
			addChild(_player2_heightLine);
			_player2_heightLine.visible=false;
			
			// quit btn
			_quit_btn.x = (stage.stageWidth - _quit_btn.width) / 2;
			_quit_btn.y = 5;
			_quit_btn.addEventListener(MouseEvent.MOUSE_DOWN, onQuitPressed);
			addChild(_quit_btn);
			
			// next_player_btn
			_next_player_btn.x = (stage.stageWidth - _next_player_btn.width) / 2;
			_next_player_btn.y = 45;
			_next_player_btn.addEventListener(MouseEvent.MOUSE_DOWN, onNextPlayerPressed);
			addChild(_next_player_btn);
			
			// winner_line
			_winner_line.x = 0;
			_winner_line.y = 150;
			addChild(_winner_line);
			
			_winner_screen.x = stage.stageWidth / 2;
			_winner_screen.y = stage.stageHeight / 2;
			addChild(_winner_screen);
			_winner_screen.visible = false;
			
			_swapPlayerMsg.x = stage.stageWidth / 2;
			_swapPlayerMsg.y = stage.stageHeight / 2;
			_swapPlayerMsg.visible = false;
			stage.addChild(_swapPlayerMsg);
			
			// icons
			addChild(_icons);
			_icons.x = stage.stageWidth;
			_icons.y = 40;
			
			drawIconsList();
			
			_drawn = true;
		}
		
		private static function roundToPrecision(number:Number, precision:int = 0):Number
		{
			var decimalPlaces:Number = Math.pow(10, precision);
			return Math.round(decimalPlaces * number) / decimalPlaces;
		}
		
		public function updateHeights():void
		{
			TextField(_player2_height.getChildByName("height2")).text = _game.Player2Name+" Tower: "+String(roundToPrecision(_game.Player2TopHeight/46,2))+"m";
			TextField(_player1_height.getChildByName("height1")).text = _game.Player1Name+" Tower: "+String(roundToPrecision(_game.Player1TopHeight/46,2))+"m";
			_player1_height.x = _game.Player1TopWidth+50;
			_player1_height.y = stage.stageHeight - _game.Player1TopHeight;
			_player1_heightLine.y = stage.stageHeight - _game.Player1TopHeight;
			_player2_height.x = _game.Player2TopWidth-50;
			_player2_height.y = stage.stageHeight - _game.Player2TopHeight;
			_player2_heightLine.y = stage.stageHeight - _game.Player2TopHeight;
			
		}
		
		public function drawIconsList():void
		{	
			if (_iconsList != null)
			{
				// remove all old icons
				for (var i:uint = 0; i < _iconsList.length; i++)
				{
					_icons.removeChild(_iconsList[i]);
				}
			}
			
			// refill icon list
			_iconsList = new Array();
			_iconsList = _game.current_player.getActionIcons();
			
			
			var l:uint = _iconsList.length;
			i = 0;
			for(; i<l; ++i)
			{
				_icons.addChild(_iconsList[i]);
				if(_game.current_player.id==0){
					_iconsList[i].x = (i * _iconsList[i].width+5)-stage.stageWidth;
				}
				else{
					_iconsList[i].x = - i * _iconsList[i].width - _iconsList[i].width;
				}
			}
			
		}
		
		private function onNextPlayerPressed(e:MouseEvent):void 
		{
			_game.changeStateToDefault();
			_game.showSwapPlayerMsg();
		}
		
		private function onQuitPressed(e:MouseEvent):void 
		{
			addChild(_quit_btn);
			_winner_screen.visible = false;
			_game.quit();
		}
		

		public function show():void 
		{
			if (!_drawn && stage)
				draw();
			
				// update stuff
			if (stage)
			{
				updateHeights();
				TextField(_player1_title.getChildByName("txt")).text =  _game.Player1Name;
				TextField(_player2_title.getChildByName("txt")).text = _game.Player2Name;
				TextField(_player1_height.getChildByName("height1")).text = _game.Player1TopHeight.toString();
				TextField(_player2_height.getChildByName("height2")).text = _game.Player2TopHeight.toString();
			}
			
			visible = true;
			hideWinnerScreen();
		}
		
		public function showSwapPlayerMsg():void
		{	
			_swapPlayerMsg.visible = true;
			
			stage.addChild(_swapPlayerMsg);
			if(_game.current_player.towerheight>=_game.next_player.towerheight){
				TextField(_swapPlayerMsg.getChildByName("msgtext")).text = _text_manager.getRandomBehindText(_game.next_player.name,_game.current_player.name);
				TextField(_swapPlayerMsg.getChildByName("msgtext")).height=100;
			}
			else{
				TextField(_swapPlayerMsg.getChildByName("msgtext")).text = _text_manager.getRandomIntheLeadText(_game.next_player.name,_game.current_player.name);
				TextField(_swapPlayerMsg.getChildByName("msgtext")).height=100;
			}
		}
		
		public function hideSwapPlayerMsg():void
		{
			_swapPlayerMsg.visible = false;
		}
		
		public function hide():void 
		{
			visible = false;
		}
		
		public function setActivePlayer(id:uint):void
		{
			_player1_title.visible = false;
			_player2_title.visible = false;
			_player2_height.visible = false;
			_player1_height.visible = false;
			_player2_heightLine.visible = false;
			_player1_heightLine.visible = false;
			if (id == 0){
				_player2_height.visible = true;
				_player2_heightLine.visible = true;
				_player1_title.visible = true;
			}
			else{
				_player1_height.visible = true;
				_player2_title.visible = true;	
				_player1_heightLine.visible = true;
			}
		}
		
		public function getWinnerLinePosition():b2Vec2
		{
			return new b2Vec2(_winner_line.x, _winner_line.y);
		}
		
		public function showWinnerScreen(player:Player):void
		{
			stage.addChild(_winner_screen);
			TextField(_winner_screen.getChildByName("txt")).text = player.name;
			_winner_screen.visible = true;
			
			stage.addChild(_quit_btn);
		}
		
		public function hideWinnerScreen():void
		{
			_winner_screen.visible = false;
		}
	}
	
}